<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/11/17
 * Time: 下午5:50
 */

class MenuController extends AdminBaseController {

    public function index(){
        $grid   = new PubGrid();
        $filter = new PubFilter();

        $pid = $this->R('pid', 'M0');
        $this->out['pid'] = $pid;
        
        $page = $this->R('page', '1');
        
        $game = $this->R('g', 'mango');
        $m = $this->R('m');
        
        $add_url = PubFunc::buildUrl('/auth/?r=menu/add&pid='.$pid);
        $this->out['add_url'] = $add_url;

        $filter->addFilter('search', 'input', PubFunc::t('菜单名'))
            ->addFilter('pid', 'hidden', '', array('value' => $pid));
            //->addButton('/auth/?r=menu/add&pid='.$pid, '<i class="fa fa-plus"></i>', 'btn btn-success');

        $params = $filter->get();
        $params['m'] = $m;
        $params['g'] = $game;
    
    	$header  = MenuService::indexHeader();
    	if(PubFunc::isAjax()){
    		$params['page'] = $this->R('page');
    		$params['length'] = $this->R('length');
    		$params['order'] = $this->R('order');
    		
    		$result = MenuService::indexData($params);
    		$this->out = array(
    				'count'     => $result['count'],
    				'data'      => $result['data'],
    				'result'    => true
    		);
    		$this->outType = 'json';
    	}else{
    		$order  = 'ParentID, SortNum';
    		$url = PubFunc::ajaxUrl("?r=menu/index", $params);
    		$grid->addGrid($header, $url, array(
    				'name'=>'数据',
    				'order'=>array($order, 'asc'),
    				'type'=>'menu/index',
    				'length'=>20,
    				'page'=>$page
    		));
    		 
    		$this->out['grid'] = $grid->show();
    		$this->out['filter'] = $filter->show();
    		$this->out['bc']        = MenuService::getBreadCrumb($pid);
    		    		
    		$this->out['game'] = $game;
    		$this->out['m'] = $m;
    		
    		$this->setTitle('菜单列表', 'Menu list');
    	}
    }
    

    public function add(){
        $form = new PubForm();

        $pid    = $this->R('pid', 'M0');

        $menuOptions = MenuService::menuOptions();
        $viewOptions = MenuService::viewOptions();
        $statusOptions = MenuService::statusOptions();

        $form->addItem('pid', 'select', PubFunc::t('父节点'), array('options' => $menuOptions, 'value' => $pid))
            ->addItem('menuname', 'input', PubFunc::t('菜单名'))
            ->addItem('sort', 'input', PubFunc::t('排序'), array('value' => MenuService::getNextSort($pid)))
            ->addItem('isview', 'select', PubFunc::t('是否显示'), array('options' => $viewOptions, 'value' => key($viewOptions)))
            ->addItem('status', 'select', PubFunc::t('状态'), array('options' => $statusOptions, 'value' => key($statusOptions)))
            ->addItem('url', 'input', PubFunc::t('链接地址'), array('style' => 'width: 400px;'))
            ->addItem('icon', 'input', PubFunc::t('菜单图标'), array('style' => 'width: 400px;'))
            ->addItem('function', 'textarea', PubFunc::t('权限方法'))
            ->addOperation('提交', 'input', 'submit', 'btn btn-primary')
            ->addOperation('返回', 'a', PubFunc::buildUrl("/auth/?r=menu/index"), 'btn btn-default')
            ->setAction(PubFunc::buildBackUrl("/auth/?r=menu/doAdd"))
            ->setName('menu');

        $this->out['form'] = $form->show();

        $this->setTitle('新增菜单', 'Add Menu');

    }

    public function doAdd(){
        $menu = $this->R('menu');

        $result = MenuService::add($menu);
        $game = $this->R('g', 'mango');
        $m = $this->R('m', 'mango');
        if ($result !== false) {
            $this->redirect("/auth/?r=menu/index&g={$game}&m={$m}&app={$menu['app']}&pid={$menu['pid']}");
        } else {
            $this->showMessage('添加失败');
        }
    }

    public function edit(){
        $form = new PubForm();
        $game = $this->R('g', 'mango');
        $m = $this->R('m');
        $id = $this->R('id');
        $menu = MenuService::getMenuById($id);
        if($menu['ParentID'] == 0){
        	$pid = 'M0';
        }else{
        	$pid = $menu['ParentID'];
        }

        $menuOptions = MenuService::menuOptions($menu['AppID']);
        $viewOptions = MenuService::viewOptions();
        $statusOptions = MenuService::statusOptions();
        $page = $this->R('page');
        $form->addItem('pid', 'select', PubFunc::t('父节点'), array('options' => $menuOptions, 'value' => $menu['ParentID']))
            ->addItem('menuname', 'input', PubFunc::t('菜单名'), array('value' => $menu['MenuName']))
            ->addItem('sort', 'input', PubFunc::t('排序'), array('value' => $menu['SortNum']))
            ->addItem('isview', 'select', PubFunc::t('是否显示'), array('options' => $viewOptions, 'value' => $menu['Visible']))
            ->addItem('status', 'select', PubFunc::t('状态'), array('options' => $statusOptions, 'value' => $menu['Status']))
            ->addItem('url', 'input', PubFunc::t('链接地址'), array('value' => html_entity_decode($menu['MenuUrl']), 'style' => 'width: 400px;'))
            ->addItem('icon', 'input', PubFunc::t('菜单图标'), array('value' => html_entity_decode($menu['MenuIcon']), 'style' => 'width: 400px;'))
            ->addItem('function', 'textarea', PubFunc::t('权限方法'), array('value' => html_entity_decode($menu['Function'])))
            ->addValue('menu[id]', $menu['MenuID'])
            ->addOperation('提交', 'input', 'submit', 'btn btn-primary')
            ->addOperation('返回', 'a', "/auth/?r=menu/index&g={$game}&m={$m}&pid={$pid}&page=".$page, 'btn btn-default')
            ->setAction(PubFunc::buildBackUrl("/auth/?r=menu/doEdit"))
            ->setName('menu');

        $this->out['form'] = $form->show();

        $this->setTitle('编辑菜单', 'Edit Menu');

        $this->out['page'] = $page;
    }

    public function doEdit(){
        $menu       = $this->R('menu');
        $result     = MenuService::edit($menu);
        $page = $this->R('page');
        $game = $this->R('g', 'mango');
        $m = $this->R('m', 'mango');
        if($result !== false){
            $this->showMessage('修改成功', "/auth/?r=menu/index&g={$game}&m={$m}&pid={$menu['pid']}&page={$page}");
        }else{
            $this->showMessage('修改失败');
        }
    }

    public function delete(){
        $id = $this->R('id');
        
        $page = $this->R('page');
        $pid = $this->R('pid');
        $game = $this->R('g', 'mango');
        $m = $this->R('m', 'mango');
        $result = MenuService::delete($id);
        if($this->isAuth !=1){
        	if($result !== false){
        		$this->showMessage('删除成功',"/auth/?r=menu/index&g={$game}&m={$m}&pid={$pid}&page={$page}");
        	}else{
        		$this->showMessage('删除失败',"/auth/?r=menu/index&g={$game}&m={$m}&pid={$pid}&page={$page}");
        	}
        }
        

    }
    
    
    public function change_visable(){
    	$id = $this->R('id');
    	$val = $this->R('val');
    	
    	$result = MenuService::change_field($id,'Visible',$val);
    	
    	$status = 1;
    	$msg = '操作成功';
    	if(!$result){
    		$status = 0;
    		$msg = '操作失败';
    	}
    	
    	$this->out = array(
    				'status' => $status,
    				'msg' => $msg,
    				'data'    => ''
    	);
    	$this->outType = 'json';
    }
    
    public function change_sortNum(){
    	$id = $this->R('id');
    	$val = $this->R('val');
    	 
    	$result = MenuService::change_field($id,'SortNum',$val);
    	
    	$status = 1;
    	$msg = '操作成功';
    	if(!$result){
    		$status = 0;
    		$msg = '操作失败';
    	}
    	$this->out = array(
    			'status' => $status,
    			'msg' => $msg,
    			'data'    => ''
    	);
    	$this->outType = 'json';
    }
    
} 